<template>
    <div class="login-bg">
        <div class="login-container">
            <div class="reset-title">重置密码</div>
            <el-form :model="param" :rules="rules" ref="register" size="large">
                <el-form-item prop="admin_email">
                    <!-- 表单输入框 -->
                    <el-input v-model="param.admin_email" placeholder="邮箱">
                        <template #prepend>
                            <el-icon>
                                <Message />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>

                <el-form-item prop="admin_pwd">
                    <el-input v-model="param.admin_pwd" placeholder="请输入新密码">
                        <template #prepend>
                            <el-icon>
                                <Lock />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>

                <el-form-item prop="admin_pwd2">
                    <el-input v-model="param.admin_pwd2" placeholder="请确认新密码">
                        <template #prepend>
                            <el-icon>
                                <Lock />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>

                <el-form-item
                    label="验证码"
                    prop="test_code"
                    :rules="[
                        { required: true, message: '验证码不能为空' },
                        { type: 'text', message: '' },
                    ]"
                    >
                    <el-input
                        v-model="param.test_code"
                        type="text"
                    />
                </el-form-item>

                <el-button class="login-btn" type="primary" size="large" @click="submitForm(register)">设置新密码</el-button>
                <p class="login-text"><el-link type="primary" @click="$router.push('/login')">返回登录</el-link></p>
            </el-form>
        </div>
    </div>
</template>

<script setup lang="ts">
// 导入创建响应式数据的韩素华
import { ref } from 'vue';
// 导入element-plus相关组件
import { ElMessage, type FormInstance, type FormRules } from 'element-plus';
// 导入API接口
import { findpwdAPI , verifyEmailApi ,removeEmailApi } from '../../api/index';
// 创建路由实例的函数
import { useRouter, useRoute } from 'vue-router';

// 用于记录表单信息的对象
const param = ref({
    admin_email: '',
    admin_pwd: '',
    admin_pwd2: '',
    test_code: ''
});
// 判断表单是否符合格式，是否为空
const rules: FormRules = {
    admin_email: [
        { required: true, message: '请输入邮箱', trigger: 'blur' },// 为空提示
        { pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/, message: '请输入正确的邮箱格式', trigger: 'blur' }// 格式提示
    ],
};

// 操作表单组件的
const register = ref<FormInstance>();

// 创建路由实例对象
const $router = useRouter();

// 创建处理路由信息的对象
const $route = useRoute();
if ($route.query.email) {
    // 数据 as string  相当于转string约束   （类型断言）
    // 接收页面携带跳转过来的参数
    param.value.admin_email = $route.query.email as string;
}
else {
    $router.push("/reset-pwd");
}
// 提交表单信息的方法
const submitForm = (formEl: FormInstance | undefined) => {
    // console.log(param)
    if (!formEl) return;
    formEl.validate(async (valid: boolean) => {
        if (valid) {
            //处理发送验证码
            let { admin_email, admin_pwd, admin_pwd2, test_code} = param.value;

            // 给用户做反馈
            if(test_code=='') {
                ElMessage.error("请输入验证码");
                return false;
            }

            let isPass ;
            if (admin_pwd != admin_pwd2) {
                ElMessage.error("两次输入的密码不一致");
            }
            else {
                // 校验邮箱验证码
                await verifyEmailApi({
                    email: admin_email,
                    test_code
                })
                .then(
                    result =>{
                        let {code , msg } = result;
                        console.log(result);
                        if(code == 200){
                            // 验证成功
                            isPass = true;
                            // 删除掉验证码
                            removeEmailApi({
                                email: admin_email,
                                test_code
                            })
                            .then(
                                res=> {
                                    console.log(res);
                                }
                            )
                        }
                        else {
                            ElMessage.error(msg);
                        }
                    }
                )
                console.log({isPass})
                // 判断邮箱验证是否通过
                if(isPass){
                    // 处理找回密码的逻辑
                    await findpwdAPI({ admin_email, new_admin_pwd: admin_pwd })
                    .then(
                        content => {
                            let { code, msg } = content;
                            if (code == 200) {
                                ElMessage.success(msg);
                                // 延迟跳转页面
                                setTimeout(() => {
                                    $router.push("/login")
                                }, 300)
                            }
                            else {
                                ElMessage.error(msg);
                            }
                        }
                    )
                }

                
            }

        } else {
            return false;
        }
    });
};

</script>

<style scoped>
.login-bg {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    background: url(../../assets/img/login-bg.jpg) center/cover no-repeat;
}

.reset-title {
    text-align: center;
    font-size: 22px;
    color: #333;
    font-weight: bold;
    margin-bottom: 10px;
}

.reset-text {
    text-align: center;
    font-size: 14px;
    color: #787878;
    margin-bottom: 40px;
}

.login-container {
    width: 450px;
    border-radius: 5px;
    background: #fff;
    padding: 40px 50px 50px;
    box-sizing: border-box;
}

.login-btn {
    display: block;
    width: 100%;
}

.login-text {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 20px;
    font-size: 14px;
    color: #333;
}
</style>
